package com.qianfeng.zhangwei.touchevent.widget;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;

/**
 * @说 明：
 * @项目名称：Sz1504Adv
 * @包 名： com.qianfeng.zhangwei.touchevent.widget
 * @类 名： MyViewPager
 * @创 建人： zhangwei
 * @创建时间：2015-08-06 11:41
 * @版 本：v1.0
 * @修 改人：
 * @修改时间：
 * @修改备注： ev.getRawX(); 控件去控件在屏幕的坐标
 */
public class MyViewPager extends ViewPager {
    private int downX;
    private int downY;
    private OnSiginTouch onSiginTouch;


    public MyViewPager(Context context) {
        super(context);
    }

    public MyViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
    }


    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return true;
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        int moveX;
        int moveY;
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                downX = (int) ev.getX();
                downY = (int) ev.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                moveX = (int) ev.getX();
                moveY = (int) ev.getY();
                Log.e("moveX", "----->>>" + moveX);
                Log.e("moveX", "----->>>" + moveY);
                break;
            case MotionEvent.ACTION_UP:
                moveX = (int) ev.getX();
                moveY = (int) ev.getY();
                Log.e("moveX", "----->>>" + moveX);
                Log.e("moveX", "----->>>" + moveY);
                if (downX == moveX && downY == moveY) {
                    if (onSiginTouch != null) {
                        onSiginTouch.onSiginTouch();
                    }
                }
//                int distanceX = Math.abs(moveX - downX);
//                int distanceY = Math.abs(moveY - downY);
//
//                if (distanceX > distanceY) {
//                    Toast.makeText(this.getContext(), "左右滑动事件触发", Toast.LENGTH_SHORT).show();
//                }
//
//                if (distanceX < distanceY) {
//                    Toast.makeText(this.getContext(), "上下滑动滑动事件触发", Toast.LENGTH_SHORT).show();
//                }
                break;
        }
        //通知父类不要处理的事件
        getParent().requestDisallowInterceptTouchEvent(true);
        return super.onTouchEvent(ev);
    }

    public OnSiginTouch getOnSiginTouch() {
        return onSiginTouch;
    }

    public void setOnSiginTouch(OnSiginTouch onSiginTouch) {
        this.onSiginTouch = onSiginTouch;
    }

    public interface OnSiginTouch {
        public void onSiginTouch();


    }


}
